/*
 * 自定义 element-ui 工具包，封装 element-ui 消息提示、警告框、消息通知、输入提示框
 * */

import Vue from 'vue'
import bfi18n from './bf-i18n'

export default {
    warningBox(msg = "", type = "warning") {
        let ev = new Vue();
        // 返回Promise，使用then()...catch()异步模式监听警告框的回调
        return new Promise(function (resolve, reject) {
            try {
                ev.$alert(
                    msg,
                    bfi18n.t("notify.messageTitle"),
                    {
                        confirmButtonText: bfi18n.t("notify.confirm"),
                        confirmButtonClass: "bf-button-confirm",
                        type: type
                    }
                ).then((confirm) => {
                    resolve(confirm);
                }).catch((cancel) => {
                    reject(cancel);
                });
            } catch (err) {
                reject(err);
            }
        });
    },
    messageBox(msg = "", type = 'info', dur = 4500) {
        let ev = new Vue();
        return new Promise(function (resolve, reject) {
            try {
                ev.$message({
                    showClose: true,
                    message: msg,
                    type: type,
                    duration: dur
                });
                resolve(1);
            } catch (err) {
                reject(err);
            }
        })
    },
    notifyBox(opts) {
        let ev = new Vue();
        let default_opts = {
            title: "",
            message: "",
            type: "info",
            duration: 4500,
            offset: 0
        };
        let __opts = Object.assign(default_opts, opts);
        return new Promise(function (resolve, reject) {
            try {
                ev.$notify(__opts);
                resolve(1);
            } catch (err) {
                reject(err);
            }
        })
    },
    promptBox(msg = "", val = "", inputValidator) {
        let ev = new Vue();
        return new Promise(function (resolve, reject) {
            try {
                ev.$prompt(msg, {
                    title: bfi18n.t("notify.messageTitle"),
                    cancelButtonText: bfi18n.t("notify.cancel"),
                    confirmButtonText: bfi18n.t("notify.confirm"),
                    inputValue: val,
                    closeOnClickModal: false,
                    closeOnPressEscape: false,
                    inputErrorMessage: bfi18n.t("notify.processMoreRecordsErrMsg"),
                    inputValidator: inputValidator || function (val) {
                        if (isNaN(val)) {
                            return false
                        }
                        return true
                    }
                }).then(({value}) => {
                    resolve(value);
                }).catch((err) => {
                    reject(err);
                });
            } catch (err) {
                reject(err);
            }
        })
    }
}
